

-- table.reverse = function(t)
--     local size = #t;
--     local half_size = math.floor(size / 2);
--     for i = 1, half_size do
--         t[i], t[size - i + 1] = t[size - i + 1], t[i];
--     end
-- end

local __G__ = {};
-- 获取表字段
function commonlib.getfield(f, G)
	if(not f) then return end
	local v = G or __G__;                                        
	for w in string.gmatch(f, "([%w_]+)(.?)") do
		if(type(v) ~= "table") then return nil end
		v = v[w];
	end
    return v;
end

-- 设置表字段
function commonlib.setfield(f, v, G)
	if(not f) then return end
	local t = G or __G__;   
	for w, d in string.gmatch(f, "([%w_]+)(.?)") do
		if d == "." then     
			t[w] = t[w] or {};  
			t = t[w];          
		else                 
			t[w] = v;           
		end
	end
end

-- 逆序表
function commonlib.reversetable(t)
    local size = #t;
    local half_size = math.floor(size / 2);
    for i = 1, half_size do
        t[i], t[size - i + 1] = t[size - i + 1], t[i];
    end
    return t;
end

-- 清空表
local clearcachetable = {};
function commonlib.cleartable(t)
	if (type(t) ~= "table") then return end

	local index = 0;
	for key in pairs(t) do 
		index = index + 1;
		clearcachetable[index] = key;
	end
	for i = 1, index do
		t[clearcachetable[i]] = nil;
	end
end

-- 获取表key列表
local __cache_keys__ = {};
function commonlib.keys(t)
	local size = #__cache_keys__;
	for i = 1, size do __cache_keys__[i] = nil end 
	local index = 0;
	for key in pairs(t) do 
		index = index + 1;
		__cache_keys__[index] = key;
	end
	return __cache_keys__, index;
end

-- 获取表value列表
local __cache_values__ = {};
commonlib.values = function(t)
	local size = #__cache_values__;
	for i = 1, size do __cache_values__[i] = nil end 
	local index = 0;
	for _, val in pairs(t) do 
		index = index + 1;
		__cache_values__[index] = val;
	end
	return __cache_values__, index;
end

-- 获取表
function commonlib.gettable(f, G)
	if(not f) then return end
	local t = G or __G__;    
	for w in string.gmatch(f, "([%w_]+)(.?)") do
		t[w] = t[w] or {};   
		t = t[w];           
	end
	return t;
end

-- 深拷贝含元表
function commonlib.deepcopy(object)
    local lookup_table = {}
    local function _copy(object)
        if type(object) ~= "table" then
            return object
        elseif lookup_table[object] then
            return lookup_table[object]
        end
        local new_table = {}
        lookup_table[object] = new_table
        for index, value in pairs(object) do
            new_table[_copy(index)] = _copy(value)
        end
        return setmetatable(new_table, getmetatable(object))
    end
    return _copy(object)
end

-- 深拷贝不含元表
function commonlib.copy(object)
	local lookup_table = {}
    local function _copy(object)
        if type(object) ~= "table" then
            return object
        elseif lookup_table[object] then
            return lookup_table[object]
        end
        local new_table = {}
        lookup_table[object] = new_table
        for index, value in pairs(object) do
            new_table[_copy(index)] = _copy(value)
        end
        return new_table;
    end
    return _copy(object)
end

-- 深拷贝源表字段
function commonlib.partialcopy(dest, src)
    local function _assign(dest, src)
	    if type(src) == "table" and type(dest) =="table"  then
			for key, value in pairs(src) do
				if(type(value) =="table") then
					if(type(dest[key]) == "table") then
						_assign(dest[key], value);
					else
						dest[key] = {};
						_assign(dest[key], value);
					end
				else
					dest[key] = value	
				end
			end
        end
    end
    return _assign(dest, src)
end

-- 深拷贝目标表不存在字段
function commonlib.mincopy(dest, src)
    local function _assign(dest, src)
	    if type(src) =="table"  and type(dest) =="table"  then
			for key, value in pairs(src) do
				if(dest[key] == nil) then
					if(type(value) =="table") then
						dest[key] = {};
						_assign(dest[key], value);
					else
						dest[key] = value	
					end
				end	
			end
        end
    end
    return _assign(dest, src)
end

-- 绑定函数到表
function commonlib.bind(fn, self)
    return function(...) return fn(self, ...) end
end

-- 表继承
function commonlib.inherit(baseClass, new_class, ctor)
	if(type(baseClass) == "string") then
		baseClass = commonlib.gettable(baseClass);
	end
	if(type(new_class) == "string") then
		new_class = commonlib.gettable(new_class);
	end
	if(not ctor and type(baseClass) == "function") then
		ctor = baseClass;
		baseClass = nil;
	end
	
	local new_class = new_class or {}
	-- new_class.__metatable will be used instead of this one at the time of new() method
    local class_mt = { __index = new_class }

	-- this ensures that the base class new function is also called. 
    function new_class:new(o)
        local o = o or {}
        
        if(baseClass) then
			-- this ensures that the constructor of all base classes are called. 
			if(baseClass.new~=nil) then
				baseClass:new(o);
			end	
        end
        setmetatable(o, rawget(new_class, "__metatable") or class_mt)
        
		-- please note inside ctor function, parent class virtual functions are not available,since meta table of parent is not set yet. 
		local ctor = rawget(new_class, "ctor");
		if(ctor) then
			ctor(o);
		end
		
        return o
    end
    new_class.ctor = ctor

    if (baseClass~=nil) then
        setmetatable(new_class, { __index = baseClass})
    end
    
	--------------------------------
    -- Implementation of additional OO properties
    --------------------------------

    -- Return the class object of the instance
    function new_class:class()
        return new_class
    end

    -- Return the super class object of the instance
    new_class._super = baseClass
    
    -- Return true if the caller is an instance of theClass
    function new_class:isa(theClass)
        local b_isa = false
        local cur_class = new_class
        while (nil ~= cur_class) and (false == b_isa) do
            if cur_class == theClass then
                b_isa = true
            else
                cur_class = cur_class._super
            end
        end

        return b_isa
    end

    return new_class
end

return {}